/* Includes ------------------------------------------------------------------*/
#include "MY_IIC_DRV.h"
#include "math.h"
#include "cap.h"

//------------------------------------------------------------------------------
#define _i2c_start           mcu_i2c_start
#define _i2c_restart         mcu_i2c_start
#define _i2c_stop            mcu_i2c_stop
#define _i2c_tx_addr         mcu_i2c_tx_addr
#define _i2c_tx_byte         mcu_i2c_tx_byte
#define _i2c_rx_byte         mcu_i2c_rx_byte
#define _delay_10ms(x)       mcu_delay_10ms(x)
//------------------------------------------------------------------------------

/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Definition of GPIOI2C constants */
#define GPIOI2C_ACK  				((uint8_t)1)
#define GPIOI2C_NACK 				((uint8_t)0)
#define I2CWRITE_MASK  			    ((uint8_t)0xFE)
#define I2CREAD_MASK   			    ((uint8_t)0x01)
/* Private macro -------------------------------------------------------------*/
/* Exported functions --------------------------------------------------------*/

/* Support size=0 for dummy write */
/**-----------------------------------------------------------------------
  * @brief  无参指令传递函数（针对仅发送指令，如测温、电容转换）
  * @param  DeviceAddr：地址  *Buff：数据存储数组  Size：发送数据总长度
  * @retval 是否传输成功
-------------------------------------------------------------------------*/
uint8_t GPIOI2C_Transmit(uint8_t DeviceAddr, uint8_t *Buff, uint8_t Size)
{uint8_t i,ret=GPIOI2C_XFER_LASTACK;

	if( _i2c_start() )
  {
	    //_i2c_stop();
	    gu32_mdc04_err = GPIOI2C_XFER_BUSERR;
		return GPIOI2C_XFER_BUSERR; 
	}		
	
  if( _i2c_tx_addr((DeviceAddr<<1) & I2CWRITE_MASK) )
	{
		
      //_i2c_stop();
      gu32_mdc04_err = GPIOI2C_XFER_ADDRNACK;
		return GPIOI2C_XFER_ADDRNACK;
		
			
	}
	
	for(i=0; i<Size; i++) 
	{
		if( _i2c_tx_byte((*Buff++)) )
		{
			if(i<(Size-1)) {ret=GPIOI2C_XFER_ABORTNACK;break;}
            else {ret=GPIOI2C_XFER_LASTNACK;}
		}
	}
	_i2c_stop();
	gu32_mdc04_err = ret;
	return ret;
}

/* Application assure size>=1. */
/**-----------------------------------------------------------------------
  * @brief  数据接收函数
  * @param  DeviceAddr：地址  *Buff：数据存储数组  Size：发送数据总长度
  * @retval 是否传输成功
-------------------------------------------------------------------------*/
uint8_t GPIOI2C_Receive(uint8_t DeviceAddr, uint8_t *Buff, uint8_t Size)
{ uint8_t i;	
	
	if( _i2c_start() )
  {
	    //_i2c_stop();
	    gu32_mdc04_err = GPIOI2C_XFER_BUSERR;
		return GPIOI2C_XFER_BUSERR; 
	}		
	
  if( _i2c_tx_addr((DeviceAddr<<1) | I2CREAD_MASK) )
	{
      //_i2c_stop();
      gu32_mdc04_err = GPIOI2C_XFER_ADDRNACK;
		return GPIOI2C_XFER_ADDRNACK;
	}
	
	for(i=0; i<(Size-1); i++) {
		(*Buff++)=_i2c_rx_byte(0);
	}
	
	(*Buff)=_i2c_rx_byte(1);
	
	_i2c_stop();
	gu32_mdc04_err = GPIOI2C_XFER_LASTNACK;
	return GPIOI2C_XFER_LASTNACK;
}


/**-----------------------------------------------------------------------
  * @brief  有参指令传递函数（针对某些指令后需要写入配置参数类型，如配置量程）
  * @param  DeviceAddr:地址 cmd:传达的指令 *pdata:随指令传递的参数 Size:发送数据总长度
  * @retval 是否传输成功
-------------------------------------------------------------------------*/
uint8_t GPIOI2C_Cmd_Write(uint8_t DeviceAddr, uint16_t cmd, uint8_t *pData, uint8_t Size)
{uint8_t i, ret=GPIOI2C_XFER_LASTACK;
	
	if( _i2c_start() )
  {
	    //_i2c_stop();
	    gu32_mdc04_err = GPIOI2C_XFER_BUSERR;
		return GPIOI2C_XFER_BUSERR; 
	}		
	
  if( _i2c_tx_addr((DeviceAddr<<1) & I2CWRITE_MASK) )
	{
        //_i2c_stop();
        gu32_mdc04_err = GPIOI2C_XFER_ADDRNACK;
		return GPIOI2C_XFER_ADDRNACK;
	}

	if( _i2c_tx_byte((uint8_t)(cmd>>8)) )
	{
	    //_i2c_stop();
	    gu32_mdc04_err = GPIOI2C_XFER_ABORTNACK;
		return GPIOI2C_XFER_ABORTNACK;	
	}
	if( _i2c_tx_byte((uint8_t)(cmd)) )
	{
	    //_i2c_stop();
	    gu32_mdc04_err = GPIOI2C_XFER_ABORTNACK;
		return GPIOI2C_XFER_ABORTNACK;	
	}	
		
	for(i=0; i<Size; i++) 
	{
		if( _i2c_tx_byte((*pData++)) )
		{
			if(i<(Size-1)) {ret=GPIOI2C_XFER_ABORTNACK; break;}
      else {ret=GPIOI2C_XFER_LASTNACK;}
		}
	}
	
	_i2c_stop();
	gu32_mdc04_err = ret;
	return ret;	
}

/**-----------------------------------------------------------------------
  * @brief  数据读取函数（针对某些指令后需要写入配置参数类型，如配置量程）
  * @param  DeviceAddr：地址  Cmd：待发送读取指令 *pdata:读取到的数据 Size：读取的数据长度
  * @retval 是否传输成功
-------------------------------------------------------------------------*/
uint8_t GPIOI2C_Cmd_Read(uint8_t DeviceAddr, uint16_t Cmd, uint8_t *pData, uint8_t Size)
{uint8_t i;
	
	if( _i2c_start() )
  {
	    //_i2c_stop();
	    gu32_mdc04_err = GPIOI2C_XFER_BUSERR;
		return GPIOI2C_XFER_BUSERR; 
	}		
	
  if( _i2c_tx_addr((DeviceAddr<<1) & I2CWRITE_MASK) )
	{
        //_i2c_stop();
        gu32_mdc04_err = GPIOI2C_XFER_ADDRNACK;
		return GPIOI2C_XFER_ADDRNACK;
	}

	if( _i2c_tx_byte((uint8_t)(Cmd>>8)) )
	{
	    //_i2c_stop();
	    gu32_mdc04_err = GPIOI2C_XFER_ABORTNACK;
		return GPIOI2C_XFER_ABORTNACK;
	}
	
	if( _i2c_tx_byte((uint8_t)(Cmd)) )
	{
	    //_i2c_stop();
	    gu32_mdc04_err = GPIOI2C_XFER_ABORTNACK;
		return GPIOI2C_XFER_ABORTNACK;
	}	
	
	_i2c_restart();
	
  if( _i2c_tx_addr((DeviceAddr<<1) | I2CREAD_MASK) )
	{
        //_i2c_stop();
        gu32_mdc04_err = GPIOI2C_XFER_ABORTNACK;
		return GPIOI2C_XFER_ABORTNACK;
	}	
	
	for(i=0; i<(Size-1); i++) {
		(*pData++)=_i2c_rx_byte(0);
	}
	
	(*pData)=_i2c_rx_byte(1);
	
	_i2c_stop();
	gu32_mdc04_err = GPIOI2C_XFER_LASTNACK;
	return GPIOI2C_XFER_LASTNACK;
}


